﻿
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Script.Serialization;

using Axis.BusinessObject.Layout;
using Axis.Dao.Layout;
using PhuLeBooze.Global;

namespace PhuLeBooze.Views.Slider.Service
{
    /// <summary>
    /// Summary description for SliderGrid
    /// </summary>
    public class SliderGrid : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {
            context.Response.Write(GetGrid(context));
        }
        protected string GetGrid(HttpContext context)
        {
            HttpRequest request = context.Request;
            HttpResponse response = context.Response;

            string _search = request["_search"];
            string numberOfRows = request["rows"];
            string pageIndex = request["page"];
            string sortColumnName = request["sidx"];
            string sortOrderBy = request["sord"];
            /* Get QueryString to xml */
            CCore cCore = new CCore();
            string inputValue = cCore.GetInputValue(context);
            CLayoutDao cLayoutDao = new CLayoutDao();
            IList<CSlideBarItem> list = cLayoutDao.GetSlideBarItem(inputValue);
            int totalRecords = cLayoutDao.LastOutputValue.TotalRows;
            int totalPages = cLayoutDao.LastOutputValue.TotalPages;
            return BuildJQGridResults(list, Convert.ToInt32(numberOfRows), Convert.ToInt32(pageIndex), totalPages);
        }
        private string BuildJQGridResults(IList<CSlideBarItem> list, int numberOfRows, int pageIndex, int totalPages)
        {
            if (list != null && list.Count > 0)
            {
                CGrid.JQGridResults result = new CGrid.JQGridResults();
                List<CGrid.JQGridRow> rows = new List<CGrid.JQGridRow>();
                foreach (CSlideBarItem item in list)
                {
                    CGrid.JQGridRow row = new CGrid.JQGridRow();
                    row.id = item.ID;
                    row.cell = new string[11];
                    row.cell[0] = item.ID.ToString();
                    row.cell[1] = item.Code;
                    row.cell[2] = item.Name;
                    row.cell[3] = item.ProjectID.ToString();
                    row.cell[4] = item.ProjectName;
                    row.cell[5] = item.Url;
                    row.cell[6] = string.Format("<img class='imgSliderImage' src='{0}' FileName='{0}'/>", item.Image);
                    row.cell[7] = item.Description;
                    row.cell[8] = item.ZOrder.ToString();
                    row.cell[9] = item.Status.ToString();
                    row.cell[10] = string.Format("<img class='imgAction' src='../../Images/16x16/edit.png' onclick=\"ShowEdit('{0}')\"/>", item.ID);
                    rows.Add(row);
                }
                result.rows = rows.ToArray();
                result.page = pageIndex;
                result.total = totalPages;
                result.records = numberOfRows;
                return new JavaScriptSerializer().Serialize(result);
            }
            else
            {
                return string.Empty;
            }
        }
        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}